/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.documents.main.action;

import com.documents.common.core.PageBean;
import com.documents.common.core.action.BaseActionSupport;
import com.documents.htmlobj.CountryObj;
import com.documents.htmlobj.ProductObj;
import com.documents.htmlobj.PropertyObj;
import com.documents.pojo.ContentType;
import com.documents.pojo.Country;
import com.documents.pojo.DocumentType;
import com.documents.pojo.Institution;
import com.documents.pojo.KnowledgeArea;
import com.documents.pojo.Product;
import com.documents.pojo.Zhishi;
import com.documents.service.country.CountryService;
import com.documents.service.document.DocumentGroupService;
import com.documents.service.document.DocumentIndexTreeService;
import com.documents.service.document.DocumentTypeService;
import com.documents.service.knowledge.ContentTypeService;
import com.documents.service.knowledge.InstitutionService;
import com.documents.service.knowledge.KnowledgeAreaService;
import com.documents.service.knowledge.ZhishiService;
import com.documents.service.product.ProductService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author Administrator
 */
public class ZhishiAction extends BaseActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = 1006L;
    private ZhishiService zhishiService;
    private DocumentTypeService docTypeService;
    private List<Zhishi> allKnowledge;
    private Map<String, String> docTypeMap = new HashMap();
    private int page = 1;
    private PageBean pageBean;
    private int pageSize = 20;
    private Zhishi knowledge;
    private String actionURI;
    private ContentTypeService ContentTypeService;
    private CountryService countryService;
    private InstitutionService institutionService;
    private KnowledgeAreaService knowledgeAreaService;
    private ProductService productService;
    private List<CountryObj> clist;
    private List<ContentType> ctlist;
    private List<KnowledgeArea> kalist;
    private List<Institution> ilist;
    private List<ProductObj> proList;
    private DocumentIndexTreeService documentIndexTreeService;

    public ContentTypeService getContentTypeService() {
        return ContentTypeService;
    }

    public void setContentTypeService(ContentTypeService ContentTypeService) {
        this.ContentTypeService = ContentTypeService;
    }

    public List<CountryObj> getClist() {
        return clist;
    }

    public void setClist(List<CountryObj> clist) {
        this.clist = clist;
    }

    public CountryService getCountryService() {
        return countryService;
    }

    public void setCountryService(CountryService countryService) {
        this.countryService = countryService;
    }

    public List<ContentType> getCtlist() {
        return ctlist;
    }

    public void setCtlist(List<ContentType> ctlist) {
        this.ctlist = ctlist;
    }

    public Map<String, String> getDocTypeMap() {
        return docTypeMap;
    }

    public DocumentIndexTreeService getDocumentIndexTreeService() {
        return documentIndexTreeService;
    }

    public void setDocumentIndexTreeService(DocumentIndexTreeService documentIndexTreeService) {
        this.documentIndexTreeService = documentIndexTreeService;
    }

    public List<Institution> getIlist() {
        return ilist;
    }

    public void setIlist(List<Institution> ilist) {
        this.ilist = ilist;
    }

    public InstitutionService getInstitutionService() {
        return institutionService;
    }

    public void setInstitutionService(InstitutionService institutionService) {
        this.institutionService = institutionService;
    }

    public List<KnowledgeArea> getKalist() {
        return kalist;
    }

    public void setKalist(List<KnowledgeArea> kalist) {
        this.kalist = kalist;
    }

    public KnowledgeAreaService getKnowledgeAreaService() {
        return knowledgeAreaService;
    }

    public void setKnowledgeAreaService(KnowledgeAreaService knowledgeAreaService) {
        this.knowledgeAreaService = knowledgeAreaService;
    }

    public List<ProductObj> getProList() {
        return proList;
    }

    public void setProList(List<ProductObj> proList) {
        this.proList = proList;
    }

    public ProductService getProductService() {
        return productService;
    }

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    public DocumentTypeService getDocTypeService() {
        return docTypeService;
    }

    public void setDocTypeService(DocumentTypeService docTypeService) {
        this.docTypeService = docTypeService;
    }

    public void setDocTypeMap(Map docTypeMap) {
        this.docTypeMap = docTypeMap;
    }

    public String getActionURI() {
        return actionURI;
    }

    public void setActionURI(String actionURI) {
        this.actionURI = actionURI;
    }

    public List<Zhishi> getAllKnowledge() {
        return allKnowledge;
    }

    public void setAllKnowledge(List<Zhishi> allKnowledge) {
        this.allKnowledge = allKnowledge;
    }

    public Zhishi getKnowledge() {
        return knowledge;
    }

    public void setKnowledge(Zhishi knowledge) {
        this.knowledge = knowledge;
    }

    public ZhishiService getZhishiService() {
        return zhishiService;
    }

    public void setZhishiService(ZhishiService zhishiService) {
        this.zhishiService = zhishiService;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public PageBean getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean pageBean) {
        this.pageBean = pageBean;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String main() throws Exception {
        return "success";
    }

    public String list() throws Exception {

        pageBean = zhishiService.getPageBean(page, pageSize, knowledge);
        createMenus();
        return SUCCESS;
    }

    public String deleteById() {

        zhishiService.delById(knowledge.getId());
        return SUCCESS;

    }

    public String addInput() throws Exception {
        actionURI = "save";
        createMenus();
        return SUCCESS;
    }

    public String save() throws Exception {
        if (knowledge.getCountry() > -1) {
            Country c = countryService.get(knowledge.getCountry());
            if (c != null) {
                knowledge.setCountryName(c.getName());
            }
            knowledge.setCountryCode(c.getCode());
        }

        if (knowledge.getProduct() > -1) {
            Product p = productService.get(knowledge.getProduct());
            if (p != null) {
                knowledge.setProductName(p.getName());
            }
            knowledge.setProductCode(p.getCode());
        }

        if (knowledge.getContentType() > -1) {
            ContentType ct = this.ContentTypeService.get(knowledge.getContentType());
            if (ct != null) {
                knowledge.setContentTypeName(ct.getName());
            }
        }


        if (knowledge.getInstitution() > -1) {
            Institution ins = this.institutionService.get(knowledge.getInstitution());
            if (ins != null) {
                knowledge.setInstitutionName(ins.getName());
            }
        }

        if (knowledge.getKnowledgeArea() > -1) {
            KnowledgeArea ka = this.knowledgeAreaService.get(knowledge.getKnowledgeArea());
            if (ka != null) {
                knowledge.setKnowledgeAreaName(ka.getName());
            }
        }
        if (knowledge.getDocTypeCode() > -1) {
            DocumentType dtype = this.docTypeService.get(knowledge.getDocTypeCode());
            if (dtype != null) {
                knowledge.setDocTypeName(dtype.getName());
            }
        }
        if(knowledge.getDocTypeName()!=null)
        {
            DocumentType dtype= this.docTypeService.findUnique("name=?", new Object[]{knowledge.getDocTypeName()});
            if(dtype!=null)
            {
                knowledge.setDocTypeCode(dtype.getId());
            }
        }
        zhishiService.save(knowledge);
        return SUCCESS;
    }

    public String update() throws Exception {
        if (knowledge.getCountry() > -1) {
            Country c = countryService.get(knowledge.getCountry());
            if (c != null) {
                knowledge.setCountryName(c.getName());
                knowledge.setCountryCode(c.getCode());
            }
        }

        if (knowledge.getProduct() > -1) {
            Product p = productService.get(knowledge.getProduct());
            if (p != null) {
                knowledge.setProductName(p.getName());
                knowledge.setProductCode(p.getCode());
            }
        }

        if (knowledge.getContentType() > -1) {
            ContentType ct = this.ContentTypeService.get(knowledge.getContentType());
            if (ct != null) {
                knowledge.setContentTypeName(ct.getName());
            }
        }


        if (knowledge.getInstitution() > -1) {
            Institution ins = this.institutionService.get(knowledge.getInstitution());
            if (ins != null) {
                knowledge.setInstitutionName(ins.getName());
            }
        }

        if (knowledge.getKnowledgeArea() > -1) {
            KnowledgeArea ka = this.knowledgeAreaService.get(knowledge.getKnowledgeArea());
            if (ka != null) {
                knowledge.setKnowledgeAreaName(ka.getName());
            }
        }
        if (knowledge.getDocTypeCode() > -1) {
            DocumentType dtype = this.docTypeService.get(knowledge.getDocTypeCode());
            if (dtype != null) {
                knowledge.setDocTypeName(dtype.getName());
            }
        }
        zhishiService.update(knowledge);
        return SUCCESS;
    }

    public String findlist() throws Exception {

        actionURI = "list";
        return SUCCESS;



    }

    public String showknowledge() throws Exception {
        knowledge = this.zhishiService.get(knowledge.getId());
        return SUCCESS;
    }

    public String updateInput() throws Exception {
        actionURI = "update";
        createMenus();
        knowledge = this.zhishiService.get(knowledge.getId());
        if (knowledge == null) {
            return ERROR;
        } else {

            return SUCCESS;
        }

    }

    public void createMenus() {
        clist = countryService.createList();
        ctlist = ContentTypeService.findAll();
        kalist = knowledgeAreaService.findAll();
        ilist = institutionService.findAll();
        proList = documentIndexTreeService.createList();

        typeList = documentTypeService.findAll();
    }
    private DocumentGroupService documentGroupService;
    private DocumentTypeService documentTypeService;
    private List<DocumentType> typeList;

    public DocumentTypeService getDocumentTypeService() {
        return documentTypeService;
    }

    public void setDocumentTypeService(DocumentTypeService documentTypeService) {
        this.documentTypeService = documentTypeService;
    }

    public List<DocumentType> getTypeList() {
        return typeList;
    }

    public void setTypeList(List<DocumentType> typeList) {
        this.typeList = typeList;
    }

    public DocumentGroupService getDocumentGroupService() {
        return documentGroupService;
    }

    public void setDocumentGroupService(DocumentGroupService documentGroupService) {
        this.documentGroupService = documentGroupService;
    }
}
